package cn.chencq.demo.thrift.test.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

import cn.chencq.demo.thrift.service.HelloWorldServiceImpl;
import cn.chencq.demo.thrift.service.IHelloWorldService;

public class HelloWorldSimpleServer {
	public static final int SERVER_PORT = 8090;

	public void startServer() {
		try {
			System.out.println("HelloWorld TSimpleServer start ....");
			// 传输层(Transport)，设置监听端口
			TServerSocket serverTransport = new TServerSocket(SERVER_PORT);

			// 处理层（Processor）
			TProcessor tprocessor = new IHelloWorldService.Processor<IHelloWorldService.Iface>(new HelloWorldServiceImpl());

			TServer.Args tArgs = new TServer.Args(serverTransport);
			tArgs.processor(tprocessor);
			tArgs.protocolFactory(new TBinaryProtocol.Factory());//协议

			// 服务层(Server)
			TServer server = new TSimpleServer(tArgs);
			// 启动监听服务
			server.serve();

		} catch (Exception e) {
			System.out.println("Server start error!!!");
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloWorldSimpleServer server = new HelloWorldSimpleServer();
		server.startServer();
	}

}